'''
Created on 6 avr. 2011

@author: paraita

    Stress: question 1.6.3

'''

import socket
import os
import random

def serveur(p):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(("",p))
    sortie = False
    while not sortie:
        datagrm, addr = s.recvfrom(1024)
        if datagrm:
            print datagrm
        if datagrm == "fini":
            sortie = True
    s.close()

def stresseur(addr,p,limite):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    i = 0
    while i < limite:
        s.sendto(str(i) + "-" + str(random.random()*100000000), (addr, p))
        i += 1
    s.sendto("fini", (addr, p))
    s.close()


if __name__ == '__main__':
    PORT = 8206
    
    pid = os.fork()
    
    if pid:
        serveur(p=PORT)
    else:
        stresseur(addr="127.0.0.1", p=PORT, limite=5000)
        
        
        